package com.canyou.utils;

import cn.hutool.core.date.DateUtil;
import com.canyou.constants.CommonConstants;

import java.time.*;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author fy
 */
public final class DateUtils extends DateUtil {

    private DateUtils() {
    }

    /**
     * java.util.Date --> java.time.LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * java.util.Date --> java.time.LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate date2LocalDate(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    /**
     * java.util.Date --> java.time.ZonedDateTime
     *
     * @param date
     * @return
     */
    public static ZonedDateTime date2ZonedDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * java.time.LocalDate --> java.util.Date
     *
     * @param localDate
     * @return
     */
    public static Date localDate2Date(LocalDate localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * java.time.ZonedDateTime --> java.util.Date
     *
     * @param zonedDateTime
     * @return
     */
    public static Date zonedDateTime2Date(ZonedDateTime zonedDateTime) {
        if (zonedDateTime == null) {
            return null;
        }
        return Date.from(zonedDateTime.toInstant());
    }

    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return zonedDateTime2Date(localDateTime.atZone(ZoneId.systemDefault()));
    }

    public static String format(ZonedDateTime zonedDateTime, String format) {
        if (StringUtils.isBlank(format)) {
            format = CommonConstants.DEFAULT_DATE_PATTERN;
        }
        return DateUtil.format(zonedDateTime2Date(zonedDateTime), format);
    }

    public static String format(ZonedDateTime zonedDateTime) {
        return format(zonedDateTime, null);
    }

    public static String format(LocalDateTime localDateTime, String format) {
        if (StringUtils.isBlank(format)) {
            format = CommonConstants.DEFAULT_DATE_PATTERN;
        }
        return DateUtil.format(localDateTime2Date(localDateTime), format);
    }

    public static String format(LocalDateTime localDateTime) {
        return format(localDateTime, null);
    }

    public static String format(LocalDate localDate, String format) {
        if (StringUtils.isBlank(format)) {
            format = CommonConstants.DEFAULT_DATE_PATTERN;
        }
        return DateUtil.format(localDate2Date(localDate), format);
    }

    public static String format(LocalDate localDate) {
        return format(localDate, null);
    }

    public static Date parse2Date(String dateStr) {
        return parse(dateStr);
    }

    public static LocalDate parse2LocalDate(String dateStr) {
        return date2LocalDate(parse2Date(dateStr));
    }

    public static LocalDateTime parse2LocalDateTime(String dateStr) {
        return date2LocalDateTime(parse2Date(dateStr));
    }

}